<?php
namespace app\api\controller;
use app\middleware\Auth;
use app\model\OrderLogisticsModel;
use app\model\OrdersModel;
use app\model\RedisModel;
use GuzzleHttp\Client;
use think\App;


class Test extends Base {
    private $req;

    private $orderModel;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->orderModel = new OrdersModel();
    }

    /*
     * 模拟更新物流
     * */
    public function updateLogistics(){
        $actionTypeList =[
            "100001"=>"揽件阶段-揽件成功",
            "100002"=>"揽件阶段-揽件失败",
            "100003"=>"揽件阶段-分配业务员",
            "200001"=>"运输阶段-更新运输轨迹",
            "300002"=>"派送阶段-开始派送",
            "300003"=>"派送阶段-签收成功",
            "300004"=>"派送阶段-签收失败",
            "400001"=>"异常阶段-订单取消",
            "400002"=>"异常阶段-订单滞留",
        ];


        if(isUnionOrderId($this->req["order_id"])){
            return $this->fail("联合订单不支持该操作");
        }

        if(empty($this->req["waybill_id"])){
            return $this->fail("请输入单号");
        }

        if(empty($this->req["action_type"])){
            return $this->fail("请输入要更新的轨迹节点");
        }

        if(!in_array($this->req["action_type"],array_keys($actionTypeList))){
            return $this->fail("更新的轨迹节点action_type 不合法");
        }

        $orderLogisticsModel = new OrderLogisticsModel();
        $list = $orderLogisticsModel->getOrderLogisticsInfo($this->req["order_id"]);

        if(empty($list)){
            return $this->fail("订单不存在或则运单号错误");
        }

        $send=[];
        $send["order_id"] = "";
        $send["delivery_id"] = "TEST";
        $send["biz_id"] = "test_biz_id";
        $send["waybill_id"] = $this->req["waybill_id"];
        $send["action_time"] = time();
        $send["action_type"] = $this->req["action_type"];
        $send["action_msg"] = $actionTypeList[$this->req["action_type"]];

        foreach ($list as $item){
            if($item["waybill_id"] == $this->req["waybill_id"]){
                $send["order_id"] = $item['wx_request']["order_id"];
            }
        }

        if(empty($send["order_id"])){
            return $this->fail("参数错误订单不存在或则运单号错误");
        }

        try {
            $response = testUpdateLogistics($send);
        }catch (\Exception $e){
            return $this->fail("网络异常：".$e->getMessage());
        }

        if ($response->getStatusCode() == 200) {
            $data = $response->getBody()->getContents();

            $data = json_decode($data, true);

            if(isset($data['errcode']) && $data["errcode"]  == 0){
                return $this->succ($data);
            }else{
                return $this->fail("请求轨迹接口错误：错误码->".$data['errcode'].",错误信息->".$data['errmsg']);
            }
        } else {
            return $this->fail("请求腾讯快递接口失败：". $response->getBody()->getContents());
        }
    }




    //取消订单的运单
    public function cancelOrderLogistic(){
        if(empty($this->req["order_id"])){
            return $this->fail("参数错误:order_id 不能为空");
        }

        if(empty($this->req["waybill_id"])){
            return $this->fail("参数错误:运单号 不能为空");
        }

        $orderInfo = $this->orderModel->getInfo($this->req["order_id"]);

        if(empty($orderInfo["logisticsInfo"])){
            return $this->fail("该订单不存在物流信息");
        }

        foreach ($orderInfo["logisticsInfo"] as $item){
            if($item["waybill_id"] == $this->req["waybill_id"]){
                $send = [];
                $send["openid"] = $item["wx_openid"];
                $send["delivery_id"] = $item["delivery_id"];
                $send["waybill_id"] = $item["waybill_id"];
                $send["order_id"] = $item["order_id"];
                try {
                    $response = cancelExpressOrder($send);
                }catch (\Exception $e){
                    return $this->fail("网络异常：".$e->getMessage());
                }

                if ($response->getStatusCode() == 200) {
                    $data = $response->getBody()->getContents();

                    $data = json_decode($data, true);

                    if(isset($data['errcode']) && $data["errcode"]  == 0){
                        return $this->succ($data);
                    }else{
                        return $this->fail("请求轨迹接口错误：错误码->".$data['errcode'].",错误信息->".$data['errmsg']);
                    }
                } else {
                    return $this->fail("请求腾讯快递接口失败：". $response->getBody()->getContents());
                }
            }
        }

        return $this->fail("操作失败：订单和运单不匹配");

    }


    public function delCache()
    {

        $cacheKey = $this->req["key"];

        $redisModel = new RedisModel();

        //删除商品缓存
        $redisModel->delete($cacheKey);

        return true;

    }


    public function delAllCache()
    {
        $cacheKey = $this->req["key"];

        $redisModel = new RedisModel();

        $redisModel->deleteAll($cacheKey);

        return true;
    }


}